package com.employee.crud.controller;

import com.employee.crud.entity.Employee;
import com.employee.crud.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
public class EmployeeController {
    @Autowired
    IEmployeeService employeeService;
    @GetMapping("/employees")
    public ResponseEntity getAll(){
        return new ResponseEntity<>(employeeService.list(null), HttpStatus.ACCEPTED);
    }
    @PostMapping("/employees")
    public ResponseEntity addEmployee(@RequestBody Employee employee){
        return new ResponseEntity<>(employeeService.save(employee),HttpStatus.CREATED);
    }
    @PutMapping("/employees/{id}")
    public ResponseEntity updateEmployee(@PathVariable Integer id,@RequestBody Employee employee) {
        if (employee.getId() == id && id.equals(employee.getId())){
            return new ResponseEntity(employeeService.saveOrUpdate(employee),HttpStatus.NO_CONTENT);
        }else {
            return new ResponseEntity("两次输入的id不一样请重新输入",HttpStatus.NO_CONTENT);
        }

    }
 @DeleteMapping("/employees/{id}")
    public ResponseEntity delteEmployee(@PathVariable Integer id){
        return new ResponseEntity(employeeService.removeById(id),HttpStatus.NO_CONTENT);
    }
}
